package com.yuan.order.controller;

import com.yuan.order.converter.OrderFormToOrderDTO;
import com.yuan.order.dto.OrderDTO;
import com.yuan.order.enums.ResultEnum;
import com.yuan.order.execption.SellException;
import com.yuan.order.form.OrderForm;
import com.yuan.order.service.BuyerService;
import com.yuan.order.service.OrderService;
import com.yuan.order.service.impl.OrderServiceImpl;
import com.yuan.order.utils.ResultVOUtils;
import com.yuan.order.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 买家端
 *
 * @Author yuan
 * @Date 2021/3/4 19:42
 */
@RestController
@RequestMapping("/buyer/order")
@Slf4j
public class BuyerOrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private BuyerService buyerService;

    /**
     * 创建订单
     *
     * @Author yuan
     * @Date 2021/3/4 20:16
     */
    @PostMapping("/create")
    public ResultVO<Map<String, Object>> create(@Valid OrderForm orderForm, BindingResult bindingResult) {
        //1.表单校验
        if (bindingResult.hasErrors()) {
            log.error("[创建订单] 参数不正确，orderForm={}", orderForm);
            throw new SellException(ResultEnum.PARAM_ERROR.getCode(),
                    bindingResult.getFieldError().getDefaultMessage());
        }
        OrderDTO orderDTO = OrderFormToOrderDTO.convert(orderForm);
        if (CollectionUtils.isEmpty(orderDTO.getOrderDetailList())) {
            log.error("[创建订单] 购物车不能为空");
            throw new SellException(ResultEnum.CART_EMPTY_ERROR);
        }
        OrderDTO result = orderService.create(orderDTO);
        Map<String, Object> map = new HashMap<>();
        map.put("orderIdd", result.getOrderId());
        return ResultVOUtils.success(map);
    }

    /**
     * 分页查询
     *
     * @Author yuan
     * @Date 2021/3/4 20:21
     */
    @GetMapping("/list")
    public ResultVO<List<OrderDTO>> list(@RequestParam("openid") String openid, @RequestParam(value = "page", defaultValue = "0") Integer page, @RequestParam(value = "size", defaultValue = "10") Integer size) {
        //1.参数校验
        if (StringUtils.isEmpty(openid)) {
            log.error("[查询订单] 查询订单列表错误，openid={}", openid);
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        //2.查询列表
        Page<OrderDTO> orderDTOPage = orderService.findList(openid, PageRequest.of(page, size));
        return ResultVOUtils.success(orderDTOPage.getContent());
    }

    /**
     * 订单详情
     *
     * @Author yuan
     * @Date 2021/3/4 20:35
     */
    @GetMapping("/detail")
    public ResultVO<OrderDTO> detail(@RequestParam("openid") String openid, @RequestParam("orderid") String orderId) {
        //1.参数校验
        if (StringUtils.isEmpty(openid) || StringUtils.isEmpty(orderId)) {
            log.error("[查询订单] 查询订单列表错误，openid={},orderId", openid, orderId);
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        OrderDTO orderDTO = buyerService.findOrder(openid, orderId);
        return ResultVOUtils.success(orderDTO);
    }

    /**
     * 订单详情
     *
     * @Author yuan
     * @Date 2021/3/4 20:35
     */
    @PostMapping("/cancel")
    public ResultVO cancel(@RequestParam("openid") String openid, @RequestParam("orderid") String orderId) {
        //1.参数校验
        if (StringUtils.isEmpty(openid) || StringUtils.isEmpty(orderId)) {
            log.error("[取消订单] 取消订单错误，openid={},orderId", openid, orderId);
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        buyerService.cancelOrder(openid, orderId);
        return ResultVOUtils.success();
    }
}
